namespace Crux.Core.Data;
/// <summary>
/// OrderExpressionBuilder
/// </summary>
public class OrderExpressionBuilder<T> : IDisposableObservable
{
    /// <summary>
    /// 
    /// </summary>
    /// <value></value>
    public bool IsDisposed { get; private set; } = false;
    private readonly List<OrderExpression<T>> _expressions = new List<OrderExpression<T>>();
    /// <summary>
    /// 
    /// </summary>
    /// <param name="expression"></param>
    /// <param name="symbol"></param>
    public void Append(Expression<Func<T, object>> expression, OrderSymbol symbol = OrderSymbol.Asc)
    {
        if (IsDisposed)
        {
            throw new ObjectDisposedException("OrderExpressionBuilderIsDisposed");
        }

        var exp = OrderExpression<T>.Create(expression, symbol);
        _expressions.Add(exp);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="expression"></param>
    /// <param name="symbol"></param>
    /// <returns></returns>
    public static OrderExpressionBuilder<T> Create(
        Expression<Func<T, object>>? expression = null,
        OrderSymbol symbol = OrderSymbol.Asc)
    {
        var builder = new OrderExpressionBuilder<T>();
        if (expression is not null)
        {
            builder.Append(expression, symbol);
        }

        return builder;
    }
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public List<OrderExpression<T>> Build()
    {
        return _expressions;
    }
    /// <summary>
    /// 
    /// </summary>
    public void Dispose()
    {
        if (IsDisposed)
        {
            return;
        }

        IsDisposed = true;

        _expressions.Clear();
        _expressions.Capacity = 1;
    }
}